<?php
namespace Admin\Controller;
use Common\Controller;
//use Think\Controller;
class BaseController extends Controller\CommonController{
	protected $root_special=array(
			// 'Tongji/index',超级管理员专用操作名单
		);
	protected $service_house=array(
			// 家政管理员的操作只能是这些，超过此范围报错
			'Index/index'
		);
	protected $white_list=array(
			// 登录验证白名单，不验证的方法
			'Base/uploadImgs',
			'Dxtj/getDxMesFromUser',
			'Index/test'
		);
   public function __construct() {
		parent::__construct();
		header('content-type:text/html;charset=utf-8');
		$operator=CONTROLLER_NAME .'/'.ACTION_NAME;
		$operator=str_replace('.html','',$operator);
		// 判断是否已登录
		if(!isset($_SESSION['admin_user']) && !in_array($operator,$this->white_list)){
			$this->redirect('Login/index');
		}
	   	$admin_user=unserialize($_SESSION['admin_user']);
		$this->assign('admin_user',$admin_user);//以后再方法中用$this->admin_user，在模板中用{$admin_user}

//	   	当前操作赋值给模版
	   $op=explode('/',$operator);
	   $selfurl=base_url();
	   $selfurl=substr($selfurl,0,strlen($selfurl)-1);
	   $selfurl.=__SELF__;
	   $selfurl=urlencode($selfurl);
	   $this->assign('operator',$operator);
	   $this->assign('controlName',$op[0]);
	   $this->assign('actionName',$op[1]);
	   $this->assign('selfurl',$selfurl);
	}
	

	/**
	 * 读取excel文件，返回需要的字段
	 * field 是数组，需要的列如 ：[A,E,F]
	 */
	public function readExcelData($pathname,$field,$start_row){
		$data=array();
		Vendor("PHPExcel.PHPExcel");   
       $objPHPExcel = new \PHPExcel();   
       $PHPReader = new \PHPExcel_Reader_Excel2007(); //默认是excel2007  
       $filePath=$pathname;
       if(!$PHPReader->canRead($filePath)){   
            $PHPReader = new \PHPExcel_Reader_Excel5(); //如果不成功的时候用以前的版本来读取  
            if(!$PHPReader->canRead($filePath)){   
                $this->_internalError('读取失败');  
            }   
        }   
       $PHPExcel = $PHPReader->load($filePath);  
       $currentSheet = $PHPExcel->getSheet(0);  
       //取得一共有多少列  
        $allColumn = $currentSheet->getHighestColumn();     
        //取得一共有多少行  
        $allRow = $currentSheet->getHighestRow();  
        //循环读取数据,默认是utf-8输出  
        for($currentRow = $start_row;$currentRow<=$allRow;$currentRow++)  
        {  
            for($currentColumn='A';$currentColumn<=$allColumn;$currentColumn++)  
            {  
            	if(in_array($currentColumn, $field)){
            		$address = $currentColumn.$currentRow;  
            		$cell_val=$currentSheet->getCell($address)->getValue(); 
            		$data[$currentRow][$currentColumn]=$cell_val; 
            	}
            } 
            
        } 
        // echo $currentSheet->getCell('B8')->getValue();die;
        return $data;
	}

	/**
	 * 导出excel
	 * data 数据二维数组
	 * tableheader 表头一维数组array('ID','小区名','坐标X','坐标Y')
	 */
	public function exportExcel($data,$tableheader,$filename){
		Vendor('PHPExcel.PHPExcel'); //导入thinkphp第三方类库
		$objPHPExcel = new \PHPExcel();
		//Excel表格式,这里简略写了8列
		$letter = array('A','B','C','D','E','F','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T');
		//表头数组
		// $tableheader = array('ID','小区名','坐标X','坐标Y');
		//填充表头信息
		for($i = 0;$i < count($tableheader);$i++) {
			$objPHPExcel->getActiveSheet()->setCellValue("$letter[$i]1","$tableheader[$i]");
		}
		for ($i = 2;$i <= count($data) + 1;$i++) {
			$j = 0;
			foreach ($data[$i - 2] as $key=>$value) {
				$objPHPExcel->getActiveSheet()->setCellValue("$letter[$j]$i","$value");
				$j++;
			}
		}
		$write = new \PHPExcel_Writer_Excel2007($objPHPExcel);
		header("Pragma: public");
		header("Expires: 0");
		header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
		header("Content-Type:application/force-download");
		header("Content-Type:application/vnd.ms-execl");
		header("Content-Type:application/octet-stream");
		header("Content-Type:application/download");;
		header('Content-Disposition:attachment;filename="'.$filename.'.xls"');
		header("Content-Transfer-Encoding:binary");
		$write->save('php://output');
	}
	

	/**
	*删除 广告图
	*/
	public function del_ads(){
		$where['ad_id']=$this->_param('ad_id','ad_id不能为空');
		$ret=M('ads')->where($where)->delete();
		if($ret){
			$this->_success('删除成功');
		}
		$this->_internalError('删除失败');
	}





	/**
	 * 通过省份获取市
	 */
	public function getCitys(){
		$provinceid=$this->_param('provinceid','provinceid不能为空');
		$city=M('s_city')->where('provinceid='.$provinceid)->select();
		$this->_success('获取城市列表成功',$city);
	}

	/**
	 * 通过城市获取区县
	 */
	public function getDistrict(){
		$cityid=$this->_param('cityid','cityid不能为空');
		$district=M('s_district')->where('cityid='.$cityid)->select();
		$this->_success('获取区县成功',$district);
	}
	/**
	 * 分页时引入
	*/
	protected function _pages($count,$limit,$p){
		$pageCount=ceil($count/$limit);//共有多少页
		$pageNumbers=getPageNumbers($p,$pageCount);//获取最少1个最多5个页码
		$this->assign('count',$count);//总记录数
		$this->assign('limit',$limit);//每页条数
		$this->assign('pageNumbers',$pageNumbers);//页码数组
		$this->assign('maxPage',$pageCount);//最大页
		$this->assign('p',$p);//当前页
	}
	/**
	 * 解析教练营业时间字符串转数组 如如9:00-11:00,13:00-15:00 转成
	 * array(
		[0]=>array('9:00','11:00'),
	 * 	[1]=>array('13:00','15:00')
	 * )
	 * 上午,晚上 转成
	 * array('上午','晚上')
	 */
	protected function section_time_to_arr($cd_time_section,$lk_time_section,$teach){
		if($teach=='场地'){
			$cd_time_section=explode(',',$cd_time_section);
			foreach($cd_time_section as $k=>$v){
				$cd_time_section[$k]=explode('-',$v);
			}
		}elseif($teach=='路考'){
			$lk_time_section=explode(',',$lk_time_section);
		}
		return array(
			'cd_time_section'=>$cd_time_section,
			'lk_time_section'=>$lk_time_section
		);
	}
	/**
	 * 解析教练营业时间字符串转字符串 如下面的数组转成9:00-11:00,13:00-15:00
	 * array(
	[0]=>array('9:00','11:00'),
	 * 	[1]=>array('13:00','15:00')
	 * )
	 * array('上午','晚上') 转成 上午,晚上
	 */
	protected function section_time_to_str($cd_time_section,$lk_time_section,$teach){
		if($teach=='场地'){
			if(empty($cd_time_section)){
				$cd_time_section='';
			}else{
				foreach($cd_time_section as $k=>$v){
					$cd_time_section[$k]=implode('-',$v);
				}
				$cd_time_section=implode(',',$cd_time_section);
			}
		}elseif($teach=='路考'){
			if(empty($lk_time_section)){
				$lk_time_section='';
			}else{
				$lk_time_section=implode('-',$lk_time_section);
			}
		}
		return array(
			'cd_time_section'=>$cd_time_section,
			'lk_time_section'=>$lk_time_section
		);
	}

	/**
	 * 上传更新包
	 * 到项目根目录下的appfiels文件夹中
	 */
	public function uploadAppfile(){
		// $this->_success('gointo uploadAppfile...');
		$dir = APP_ROOT . '/appfiles/';
		$url=base_url().'appfiles/';//访问路径
		if(empty($_FILES)){
			$this->_internalError('请选择文件');
		}
		foreach ($_FILES as $file) {
			$typeinfo=pathinfo($file['name']);
			$realname=$typeinfo['basename'];
			if (! file_exists ($dir)) {
				mkdir($dir, 0777, true);
			}
			if(move_uploaded_file($file['tmp_name'],$dir.$realname)){
				$this->_success('安装包上传成功',array('appurl'=>$url.$realname));
			}
		}
	}

	
	


	 

	


	


	



}